from Base import message, intersect, SS_VALID,SS_INVALID
from ADRG import PB1,PC1,PD1,PJ1,PK1,PR1,PS1,PS2,PS3,PS4,PU1,PV1

def group(record):
  mdc_zd = []
  dept_list = []

  if not record.ageDay or not (True and int(record.ageDay)<29):
    return ''

    
  message('符合MDCP入组条件，匹配规则：主诊断匹配')

  result=PB1.group(record)
  if result:
    return result
  result=PC1.group(record)
  if result:
    return result
  result=PD1.group(record)
  if result:
    return result
  result=PJ1.group(record)
  if result:
    return result
  result=PK1.group(record)
  if result:
    return result

  if record.ssList and record.ssList[0] in SS_VALID and record.ageDay!=None and record.ageDay<29:
    message('符合PQY入组条件，存在有效手术操作：' + record.ssList[0])
    return 'PQY'
  
  if record.ssList and intersect(SS_INVALID, record.ssList[0]):
    record.ssList = []

  result=PR1.group(record)
  if result:
    return result
  result=PS1.group(record)
  if result:
    return result
  result=PS2.group(record)
  if result:
    return result
  result=PS3.group(record)
  if result:
    return result
  result=PS4.group(record)
  if result:
    return result
  result=PU1.group(record)
  if result:
    return result
  result=PV1.group(record)
  if result:
    return result
  elif record.ageDay!=None and record.ageDay<29:
    message('不符合MDCP的ADRG入组条件')
    return 'P00'

  message('不符合MDCP的ADRG入组条件')
  return 'P00'
